package com.aaa.system.service.impl;

import com.aaa.system.entity.Permission;
import com.aaa.system.mapper.PermissionMapper;
import com.aaa.system.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author 闫克起
 * @since 2023-06-20
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public List<Permission> findLeftMenuByUserId(String id) {
        List<Permission> permissions=permissionMapper.findLeftMenuByUserId(id);
        //1. 查询一级菜单
        List<Permission> first = permissions.stream().filter(item -> item.getPid().equals("1")).collect(Collectors.toList());
        //2.查询一级菜单下的二级菜单
        for(Permission f:first){
            List<Permission> second=new ArrayList<>();
            for(Permission p:permissions){ //所有菜单
                 if(f.getId().equals(p.getPid())){
                     second.add(p);
                 }
            }
            f.setChildren(second);
        }
        return first;
    }
}























